package de.hu.wbi.histoner.train;

/**
Copyright 2011,2012 Philippe Thomas
This file is part of histoner.

HistoNer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

HistoNer  is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with HistoNer .  If not, see <http://www.gnu.org/licenses/>.
*/


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class can be used to bootstrap new pattern from the original (non-annotated) pattern file.
 * This is done by generating shorter (sub)patterns from longer pattern. However, the immense number of new bootstrapped pattern renders the manual annotation impossible
 * In a first test I annotated the best 1000 pattern, but the method produces a lot of unspecific pattern. 
 * @author philippe
 *
 */
public class PatternBootstrap {

	public static String in= "data/pattern/pattern.raw"; //The raw pattern
	public static String out= "data/pattern/bootstrap.raw"; //The bootstrapped pattern
	public static int nToken = 5;
	public static Pattern whitespace = Pattern.compile("\\s");		

	public static void main(String[] args) {
	
		ArrayList<String> bootstraps  = new ArrayList<String>();
		
		for(String string:loadPattern(in)){
			ArrayList<String> list= bootstrapPattern(string,nToken);
			if(list != null && list.size() >0)
				bootstraps.addAll(list);
		}
		
		System.out.println(bootstraps.size());
		try {
			writeArraytoFile(bootstraps,out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	
	private static void writeArraytoFile(ArrayList<String> list, String file) throws IOException{
		
		BufferedWriter bw = null;
		bw = new BufferedWriter(new FileWriter(file));


		for(String string:list){
			bw.append(string +"\t" +countToken(string) +"\n");
		}
		bw.close();
		
	}
	
	/**
	 * Bootstraps all Pattern in one string
	 * @param text
	 * @return
	 */
	private static ArrayList<String> bootstrapPattern(String text, int maxToken){
		ArrayList<String> res = new ArrayList<String>();
		
		Pattern p = Pattern.compile("<mod>|<aa>|<number>");		
		Matcher outerMatcher= p.matcher(text);		
		
		while(outerMatcher.find()){
			String substring= text.substring(outerMatcher.start());
			
			Matcher innerMatcher= p.matcher(substring);
			int aa=0; int mod=0; int number=0;		
			while(innerMatcher.find()){
				
				if(innerMatcher.group().equals("<mod>"))
					mod++;
				
				else if(innerMatcher.group().equals("<aa>"))
					aa++;
				
				else if(innerMatcher.group().equals("<number>"))
					number++;
				else
					System.err.println("Strange error");
				
				if(mod >=1 && aa >=1 && number >=2 && countToken(substring.substring(0, innerMatcher.end())) <= maxToken)
					res.add(substring.substring(0, innerMatcher.end()));
		    }			
		}
		
		return res;
	}
	
	private static int countToken(String s){
		
		Matcher outerMatcher= whitespace.matcher(s);		
		
		int count=0;
		while(outerMatcher.find())
			count++;
		
		return count;
	}
	
	/**
	 * Loads the pattern generated using rawpatterngenerator and stores them into a ArrayList
	 * @param file
	 * @return
	 */
	private static ArrayList<String> loadPattern(String file){
		ArrayList<String> result = new ArrayList<String>();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		String text= null;
		try {
			while (( text= br.readLine()) != null){
				result.add(text.trim());
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
					
		return result;
	}

}
